<?php
/**
 * Addmodule Controller
 *
 * Administrator Management
 *
 *
 * @version $Id: AddmoduleController.php 77 2010-04-25 07:28:48Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package admin
 * @subpackage Controller
 */
class Admin_AddmoduleController extends Zend_Controller_Action
{
    public function init()
    {
        /*
         //Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
         $contextSwitch = $this->_helper->contextSwitch;
         if (!$contextSwitch->hasContext('ajax')) {

         $contextSwitch->addContext('ajax', array('suffix' => 'ajax'))
         //       ->addActionContext('moduleform', 'ajax')
         ->addActionContext('followup', 'ajax')
         ->addActionContext('display', 'ajax')
         ->addActionContext('active', 'ajax')
         ->addActionContext('active-data', 'ajax')
         ->addActionContext('active-data-count', 'ajax')
         ->initContext();
         }
         */

    }

    function indexAction()
    {
        $module = new Default_Model_Module();
        // 未安裝模組
        $this->view->all_modules = $module->getYetInstallModule();
        // 授權類別
        //        $this->view->roleKind = $module->getRoleKind();
        // 分類選單
        //        $this->view->moduleMenu = $module->getModuleMenu();
    }

    function addAction()
    {

        if ($this->_request->isPost()) {
            $id = $this->_request->getParam('module_id', '');
            if ($id=='') {
                $this->view->errorTitle = '安裝錯誤';
                $this->view->errorMessage = '無效的模組名稱';
                $this->_redirect('/', 'error', 'core');
                return;
            }
            $title = $this->_request->getParam('title');
            $published = $this->_request->getParam('published');
            $parent_id = $this->_request->getParam('parent_id');
            $f = new Zend_Filter_StripTags();
//            $id = strtolower($f->filter($id));
            $id = $f->filter($id);
            $title= $f->filter($title);
            $parent_id = $f->filter($parent_id);
            // 存入資源
            $resource = new Default_Model_Resources();
            $resource->id= $id;
            $resource->title = $title;
            $resource->sort = 0;
            $resource->published = $published;
            $resource->kind = 'module';
            $resource->save();

            // 存入模組
            $module_xml = Bootstrap::getPath('modules').'/'.$module.'/'.$id.'/module.xml';
            if (is_file($module_xml)) {
                $res = new Zend_Config_Xml($module_xml);
                $modules = new Default_Model_Modules();
                $modules->id = $id;
                $modules->author = (string)$res->author;
                $modules->version = (string)$res->version;
                $modules->license = (string)$res->license;
                $modules->authorEmail = (string)$res->authorEmail;
                $modules->authorUrl = (string)$res->authorUrl;
                $modules->save();
            }

            // 存入模組存取資源
            $aclResources = new Default_Model_AclResources();
            $aclResources->resource_id = $id;
            $aclResources->parent_id = $parent_id;
            $aclResources->save();

            // 存入角色認證
            $roleId = $this->_request->getParam('temp_id', 0);
            if ($roleId) {
                $aclAccess = new Default_Model_AclAccess();
                $aclAccess->resource_id = $aclResources->id;
                $aclAccess->role_id = $roleId;
                $aclAccess->allow = 1;
                $aclAccess->save();
            }
            //TODO 刪除 ACL Cache  ,
            //升級時,教師職稱及所在處室
            //須加入  groups ,授權才會正常
            // 重設 acl cache

            $this->_helper->ResetAclResources();
            $this->_redirect("admin/addmodule/index");

        }
    }


    function installformAction()
    {
        $this->getHelper('layout')->disableLayout();
        $module = $this->_request->getParam('m', '');
        if ($module) {
            $module_xml = Bootstrap::getPath('modules').'/'.$module.'/module.xml';
            if (is_file($module_xml)) {
                $res = new Zend_Config_Xml($module_xml);
                $this->view->moduleData = $res->toArray();
                //        echo $this->_helper->json($res);
            }
        }


        $module = new Default_Model_Module();
        // 未安裝模組
        $this->view->all_modules = $module->getYetInstallModule();
        // 授權類別
        $this->view->roleKind = $module->getRoleKind();
        // 分類選單
        $this->view->moduleMenu = $module->getModuleMenu('',$this->view->translate('topPage'));
    }

    /**
     * 取得不同授權群組
     * @return unknown_type
     */
    function getitemAction()
    {
        $kind = $this->_request->getParam('kind');
        $module = new Default_Model_Module();
        $data = $module->getPerrItem($kind);
        $this->_helper->json($data);
        //
        //        $dojoData = new Zend_Dojo_Data('sn', $data);
        //        $dojoData->setLabel('item');
        //        echo $dojoData->toJson();
        //        exit;
    }
}